/**
 * create by young (young2f@outlook.com) on 2023/4/16
 */
'use strict';
const _stringUtils = require('../utils/stringUtils');
const _fileUtils = require('../utils/fileUtils');
const _ = require('lodash');
module.exports = class {
    constructor(app) {
        this.app = app;
        this.config = app.config;
    }

    async suInit() {
        const user = await this.app.dbs.find('User', {userType: 'su'});

        if (user && user.length) {
            return;
        }
        const su = {
            username: 'super_admin',
            userType: 'su',
            password: _stringUtils.getCryptoPassword('afei@168'),
            isAdmin: true,
            isDeleted: false,
            tenantCode: _stringUtils.getCode('TEN'),
            userCode: _stringUtils.getCode('HM')
        };
        return this.app.dbs.insert('User', su);
    }

    async initLabelDb(tenantCode, tenantName) {
        /**
         * 设备档案的初始字段
         */
        const initFile = this.app.config.infoPath + '/initLabel.json';
        let array = await _fileUtils.isExist(initFile) ? await _fileUtils.readFile2json(initFile) : initLabels;
        _.map(array, async item => {
            item.tenantCode = tenantCode;
            item.tenantName = tenantName;
            await this.app.dbs.insert('DLabel', item);
        });
    }

    async initTenantThingModel() {
        const arr = await this.app.dbs.find('Tenant', {});
        _.map(arr, async item => {
            const {tenantCode, tenantName} = item;
            const res = await this.initThing(tenantCode, tenantName);
        });
    }

    async initThing(tenantCode, tenantName) {
        _.map(thingModel, async (item) => {
            const {code} = item;
            const model = await this.app.dbs.find('ThingModel', {tenantCode, tenantName, code});
            console.log('model',code, model.length);
            if (model.length === 0) {
                item.tenantCode = tenantCode;
                item.tenantName = tenantName;
                const res = await this.app.dbs.insert('ThingModel', item);
            }
        });
    }
};

const initLabels = [
    {
        'isDeleted': false,
        'enableArea': [
            'table',
            'detail',
            'search'
        ],
        'selectOption': 'material',
        'required': true,
        'title': '',
        'key': 'code',
        'label': '订单编码',
        'type': 'String',
        'inputType': 'input',
        'table': 'salesOrder',
        'isCode': true,
        'isInitKey': true
    },
    {
        'isDeleted': false,
        'enableArea': [
            'table',
            'detail',
            'search'
        ],
        'selectOption': 'material',
        'required': true,
        'title': '',
        'key': 'materName',
        'label': '物料',
        'type': 'String',
        'inputType': 'selectOption',
        'table': 'salesOrder',
        'isInitKey': true
    }
    ,

    {
        'isDeleted': false,
        'enableArea': [
            'table',
            'detail',
            'search'
        ],
        'selectOption': 'material',
        'required': true,
        'title': '',
        'key': 'code',
        'label': '采购单编码',
        'type': 'String',
        'inputType': 'input',
        'table': 'procureOrder',
        'isCode': true,
        'isInitKey': true
    }
    ,
    {
        'isDeleted': false,
        'enableArea': [
            'table',
            'detail',
            'search'
        ],
        'selectOption': 'material',
        'required': true,
        'title': '',
        'key': 'code',
        'label': '物料编码',
        'type': 'String',
        'inputType': 'input',
        'table': 'material',
        'isCode': true,
        'isInitKey': true
    },
    {
        'isDeleted': false,
        'enableArea': [
            'table',
            'detail',
            'search'
        ],
        'selectOption': 'material',
        'required': true,
        'title': '',
        'key': 'name',
        'label': '物料名称',
        'type': 'String',
        'inputType': 'input',
        'table': 'material',
        'isInitKey': true
    }
];

const thingModel = [
    {
        code: "chillerModel",
        name: "制冷主机",
        points: [
            {"path": "ChillerHandAutomatic", "name": "主机手自动", "unit": ""},
            {"path": "ChillerJog", "name": "主机点动", "unit": ""},
            {"path": "ActiveCoolHeatSetpointTemperaturePV", "name": "冷冻水设定值反馈", "unit": "Hz"},
            {"path": "ActiveCurrentLimitSetpointPV", "name": "电流百分比设定值反馈", "unit": "Hz"},
            {"path": "EvapEnteringWaterTemp", "name": "冷冻水进水温度", "unit": "℃"},
            {"path": "EvapLeavingWaterTemp", "name": "冷冻水出水温度", "unit": "℃"},
            {"path": "ActualRunningCapacity", "name": "运行负载电流百分比", "unit": "%"},
            {"path": "EvapRfgtPressure", "name": "蒸发器冷媒压力", "unit": "P"},
            {"path": "CondenserRfgtPressure", "name": "冷凝器冷媒压力", "unit": "P"},
            {"path": "EvapSatRfgtTemp", "name": "蒸发器冷媒温度", "unit": "℃"},
            {"path": "CondSatRfgtTemp", "name": "冷凝器冷媒温度", "unit": "℃"},
            {"path": "HighSideOilPressure-Compressor1A", "name": "高压侧油压", "unit": "P"},
            {"path": "CompressorStarts", "name": "压缩机启动次数", "unit": ""},
            {"path": "CompressorRunningTime", "name": "压缩机运行时间", "unit": "h"},
            {"path": "CondEnteringWaterTemp", "name": "冷却水进水温度", "unit": "℃"},
            {"path": "CondLeavingWaterTemp", "name": "冷却水出水温度", "unit": "℃"},
            {"path": "EvaporatorApproachTemperatureCircuit1", "name": "蒸发器趋近温度", "unit": "℃"},
            {"path": "CondenserApproachTemperatureCircuit1", "name": "冷凝器趋近温度", "unit": "℃"},
            {"path": "lastdiagnosticCode", "name": "最新故障代码", "unit": ""},
            {"path": "LocalSetpointControl", "name": "主机本地状态", "unit": ""},
            {"path": "ChillerRunning", "name": "压缩机运行状态", "unit": ""},
            {"path": "EvaporatorWaterFlow", "name": "蒸发器水流状态", "unit": ""},
            {"path": "DiagnosticShutdownPresent", "name": "主机关机故障", "unit": ""},
            {"path": "CondenserWaterFlow", "name": "冷凝器水流状态", "unit": ""},
            {"path": "FrontPanelAutoStop", "name": "主机面板自动/停止状态", "unit": ""},
            {"path": "ActiveCoolHeatSetpointTemperatureSV", "name": "冷冻水出水温度设定", "unit": "℃"},
            {"path": "ChillerRun", "name": "主机运行", "unit": ""}
        ]
    },
    {
        code: "pumpModel",
        name: "水泵",
        points: [
            {"path": "coolingPumpRun", "name": "水泵运行", "unit": ""},
            {"path": "coolingPumpFail", "name": "水泵故障", "unit": ""},
            {"path": "coolingPumpRemote", "name": "水泵远程", "unit": ""},
            {"path": "coolingPumpHandAutomatic", "name": "水泵手自动", "unit": "Hz"},
            {"path": "coolingPumpFrequencyHandAutomatic", "name": "水泵频率手自动", "unit": ""},
            {"path": "coolingPumpJog", "name": "水泵点动", "unit": ""},
            {"path": "coolingPumpClearTime", "name": "水泵清零时间", "unit": ""},
            {"path": "coolingPumpHandFrequency", "name": "水泵手动频率", "unit": "Hz"},
            {"path": "coolingPumpRunTime", "name": "水泵运行时间", "unit": ""},
            {"path": "coolingPumpFrequencyPV", "name": "水泵频率反馈", "unit": "Hz"}
        ]
    }, {
        code: "towerModel",
        name: "冷却塔",
        points: [
            {"path": "coolingTowerRun", "name": "水泵运行", "unit": ""},
            {"path": "coolingTowerFail", "name": "水泵故障", "unit": ""},
            {"path": "coolingTowerRemote", "name": "水泵远程就地", "unit": ""},
            {"path": "coolingTowerFrequencyPV", "name": "水泵频率反馈", "unit": "Hz"},
            {"path": "coolingTowerHandFrequency", "name": "水泵手动频率", "unit": ""},
            {"path": "coolingTowerRunTime", "name": "水泵运行时间", "unit": ""}
        ]
    }, {
        code: "powerModel",
        name: "智能电表",
        points: [
            {"path": "PositiveActiveEnergy", "name": "正向有功电能", "unit": "kWh"},
            {"path": "ReverseActiveEnergy", "name": "反向有功电能", "unit": "kWh"},
            {"path": "TotalActiveEnergy", "name": "总正向有功电能", "unit": "kWh"},
            {"path": "PositiveReactiveEnergy", "name": "正向无功电能", "unit": "V"},
            {"path": "ReverseReactiveEnergy", "name": "反向无功电能", "unit": "kWh"},
            {"path": "AVoltage", "name": "A相电压", "unit": "V"},
            {"path": "BVoltage", "name": "B相电压", "unit": "V"},
            {"path": "BVoltage", "name": "C相电压", "unit": "V"},
            {"path": "AveragePhaseVoltage", "name": "平均相电压", "unit": "V"},
            {"path": "ABLineVoltage", "name": "AB线电压", "unit": "V"},
            {"path": "BCLineVoltage", "name": "BC线电压", "unit": "V"},
            {"path": "CALineVoltage", "name": "CA线电压", "unit": "V"},
            {"path": "AverageLineVoltage", "name": "平均线电压", "unit": "V"},
            {"path": "ACurrent", "name": "A相电流", "unit": "A"},
            {"path": "BCurrent", "name": "B相电流", "unit": "A"},
            {"path": "CCurrent", "name": "C相电流", "unit": "A"},
            {"path": "AveragePhaseCurrent", "name": "平均相电流", "unit": "A"}
        ]
    }];


